﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.Common;
using WSData_RENIEC.Dominio;
using WSData_RENIEC.Persistencia;

namespace WSData_RENIEC
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "wsReniec" in code, svc and config file together.
    public class wsReniec : IwsReniec
    {
        
        public Persona ObtenerPersonaxDNI(string DNI)
        {
            Persona ListaPersona = new Persona();

            try
            {
                string StoreProcedure = "spWS_Persona_BuscarxDNI";
                using (DbConnection con = Util.dpf.CreateConnection())
                {
                    con.ConnectionString = Util.GetConexion;
                    using (DbCommand cmd = Util.dpf.CreateCommand())
                    {
                        cmd.Connection = con;
                        cmd.CommandText = StoreProcedure;
                        cmd.CommandType = CommandType.StoredProcedure;
                        DbParameter param = Util.dpf.CreateParameter();
                        param.Value = DNI;
                        param.ParameterName = "pDNI";
                        cmd.Parameters.Add(param);
                        con.Open();

                        using (DbDataReader dr = cmd.ExecuteReader())
                        {
                            if (dr.Read())
                            {
                                ListaPersona.DNI = (string)dr["DNI"];
                                ListaPersona.ApePat = (string)dr["ApePaterno"];
                                ListaPersona.ApeMat = (string)dr["ApeMaterno"];
                                ListaPersona.Nombre = (string)dr["Nombre"];
                                ListaPersona.FecNac = (DateTime)dr["FecNacimiento"];
                            }
                        }

                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return ListaPersona;
        }


        public List<Persona> ObtenerPersonaxApellido(string ApePat, string ApeMat)
        {
            List<Persona> ListaPersona = new List<Persona>();
            try
            {
                string StoreProcedure = "spWS_Persona_BuscarxApellido";
                using (DbConnection con = Util.dpf.CreateConnection())
                {
                    con.ConnectionString = Util.GetConexion;
                    using (DbCommand cmd = Util.dpf.CreateCommand())
                    {
                        cmd.Connection = con;
                        cmd.CommandText = StoreProcedure;
                        cmd.CommandType = CommandType.StoredProcedure;
                        
                        DbParameter param = Util.dpf.CreateParameter();
                        param.Value = ApePat;
                        param.ParameterName = "pApePaterno";
                        cmd.Parameters.Add(param);

                        DbParameter param2 = Util.dpf.CreateParameter();
                        param2.Value = ApeMat;
                        param2.ParameterName = "pApeMaterno";
                        cmd.Parameters.Add(param2);
                        con.Open();

                        using (DbDataReader dr = cmd.ExecuteReader())
                        {
                            while (dr.Read())
                            {
                                ListaPersona.Add(new Persona((string)dr["DNI"], (string)dr["ApePaterno"], (string)dr["ApeMaterno"], (string)dr["Nombre"], (DateTime)dr["FecNacimiento"]));
                            }
                        }

                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return ListaPersona;
        }

    }
}
